package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

func HandelDt(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	//该日期的前一天
	yl, ml, dl := tools.GetNextDay(year, month, day, -1)
	timeLast := yl + "-" + ml + "-" + dl
	var foreList models.ForeList
	db.Where("code = ?", "dt").First(&foreList)
	var foreDetail models.ForeDetailJson
	db.Where("foreListId = ? AND time = ?", foreList.Id, timeLast).First(&foreDetail)
	var dtData = models.Forecast{}
	json.Unmarshal(foreDetail.Data, &dtData)
	for i, report := range forecast.Reports {
		if i == 0 {
			for j := 2; j < 5; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j-1)
				report.Data[j][0].Value = m + "月" + d + "日"
				//timeNew := y + "年" + m + "月" + d + "日"
				//月和日补0
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				timeNew := y + "-" + m + "-" + d
				//这部分操作与上传文件有关，时间那些
				var tide models.TwcTide
				db.Where("date = ? AND station = ?", timeNew, "大门").First(&tide)
				if tide.Id != 0 {
					report.Data[j][1].Value = tide.TimeHigh1
					report.Data[j][2].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh1)/100, 2)
					report.Data[j][3].Value = tide.TimeHigh2
					report.Data[j][4].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh2)/100, 2)
					report.Data[j][5].Value = tide.TimeLow1
					report.Data[j][6].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow1)/100, 2)
					report.Data[j][7].Value = tide.TimeLow2
					report.Data[j][8].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow2)/100, 2)
				} else {
					for k := 1; k < 9; k++ {
						report.Data[j][k].Value = ""
					}
				}
			}
		} else {
			for j := 1; j < 4; j++ {
				_, m, d := tools.GetNextDay(year, month, day, j)
				report.Data[0][j].Value = m + "月" + d + "日"
			}
			if len(dtData.Reports) != 0 {
				for _, idx := range []int{1, 2, 3, 5, 6, 7} {
					report.Data[idx] = dtData.Reports[1].Data[idx]
				}
			}
		}
	}
}

func HandelDtzb(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	//该日期的前一天
	yl, ml, dl := tools.GetNextDay(year, month, day, -1)
	timeLast := yl + "-" + ml + "-" + dl
	var foreList models.ForeList
	db.Where("code = ?", "dtzb").First(&foreList)
	var foreDetail models.ForeDetailJson
	db.Where("foreListId = ? AND time = ?", foreList.Id, timeLast).First(&foreDetail)
	var dtData = models.Forecast{}
	json.Unmarshal(foreDetail.Data, &dtData)
	for i, report := range forecast.Reports {
		if i == 1 {
			for j := 2; j < 9; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j-1)
				report.Data[j][0].Value = m + "月" + d + "日"
				//timeNew := y + "年" + m + "月" + d + "日"
				//月和日补0
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				timeNew := y + "-" + m + "-" + d
				//这部分操作与上传文件有关，时间那些
				var tide models.TwcTide
				db.Where("date = ? AND station = ?", timeNew, "大门").First(&tide)
				if tide.Id != 0 {
					report.Data[j][1].Value = tide.TimeHigh1
					report.Data[j][2].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh1)/100, 2)
					report.Data[j][3].Value = tide.TimeHigh2
					report.Data[j][4].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh2)/100, 2)
					report.Data[j][5].Value = tide.TimeLow1
					report.Data[j][6].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow1)/100, 2)
					report.Data[j][7].Value = tide.TimeLow2
					report.Data[j][8].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow2)/100, 2)
				} else {
					for k := 1; k < 9; k++ {
						report.Data[j][k].Value = ""
					}
				}
			}
		} else {
			for j := 1; j < 8; j++ {
				_, m, d := tools.GetNextDay(year, month, day, j)
				report.Data[j][0].Value = m + "月" + d + "日"
				if len(dtData.Reports) != 0 {
					report.Data[j][1].Value = dtData.Reports[0].Data[j][1].Value
					report.Data[j][2].Value = dtData.Reports[0].Data[j][2].Value
				}
			}
		}
	}
}
